import Mock from 'mockjs'
import StringUtils from '../../utils/stringUtils.js' 

let List = []
const count = 5
const roles = ['超级管理员', '系统操作员', '业务操作员', '普通用户', '客服'];
const rolekeys = ['admin', 'system', 'yewu', 'common', 'kefu'];
Mock.Random.extend({
    roles: roles,
    role: function(date){  //随机生成一个角色
        return this.pick(this.roles)
    }
})

const menuList =[{
	id: 1,
	label: '系统管理',
	children:[{
		id: 11,
		label: '用户管理',
		},
		{
		id: 12,
		label: '角色管理',
		},
		{
		id: 13,
		label: '菜单管理',
		}],
	},
	{
		id: 2,
		label: '业务管理',
		children:[{
			id: 21,
			label: '商品管理',
		},
		{
			id: 22,
			label: '客户管理',
		}],
	},
]

for(let i=0;i<count;i++){
	List.push(
		Mock.mock({
			roleId: Mock.Random.increment(1),
			roleName: roles[i], //Mock.Random.role(),
			rolekey: rolekeys[i],
			rolemenu: [1,11,12,13,2,21,22],
			remark: '角色 role'+i+' 的权限说明'
		})
	)
}

export default {
	/**
	 * 获取数据
	 * 要带参数name,age,limt;name可以不填，page,limit有默认值
	 */
	getRoleList: config =>{
		const {keyword, page = 1, limit = 10} = StringUtils.param2Obj(config.url)
		const mockList = List.filter(t => {
			if(keyword && t.rolename.indexOf(keyword) === -1 && t.rolekey.indexOf(keyword) === -1) return false
			return true
		})
		const pageList = mockList.filter((item,index) => index < limit * page && index >= limit * (page - 1))
		return {
			code: 20000,
			count: mockList.length,
			list: pageList
		}
	},
	
	/**
	 * 增加
	 */
	addRole: config => {
		const {rolename, rolekey, remark, rolemenu } = JSON.parse(config.body)
		List.unshift({
			id: Mock.Random.guid(),
			rolename: rolename,
			rolekey: rolekey,
			rolemenu: rolemenu,
			remark: remark,
		})
		return {
			code: 20000,
			data: {
				message: '添加成功'
			}
		}
	},
	
	/**
	 * 修改
	 */
	updateRole: config => {
		const { id, rolename, rolekey, remark ,rolemenu } = JSON.parse(config.body)
		List.some(u=>{
			if(u.id === id) {
				u.rolename = rolename
				u.rolekey = rolekey
				u.remark = remark
				u.rolemenu = rolemenu
				return true
			}
		})
		return {
			code: 20000,
			data: {
				message: '编辑成功'
			}
		}
	},
	
	/**
	 * 删除
	 */
	delRole: config => {
		const {id} =JSON.parse(config.body) 
		if (!id){
			return {
				code: -999,
				message: '参数不正确'
			}
		}
		else{
			List = List.filter(u=>u.id !== id)
			return {
				code: 20000,
				message: '删除成功'
			}
		}
	},
	
	// getMenuTree: config =>{
	// 	return {
	// 		code: 20000,
	// 		count: menuList.length,
	// 		list: menuList
	// 	}
	// },
	
}